/*!
    \file    main.c
*/

/*
    Copyright (c) 2020, GigaDevice Semiconductor Inc.

    Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this 
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice, 
       this list of conditions and the following disclaimer in the documentation 
       and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors 
       may be used to endorse or promote products derived from this software without 
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.
*/

#include "gd32f30x.h"
#include <stdio.h>
#include "version.h"
#include "cpuusage.h"

#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"

#include "DevicesDelay.h"
#include "DevicesTimer.h"
#include "DevicesUart.h"

#include "taskSystem.h"

#include "main.h"


/*!< Uncomment the following line if you need to relocate your vector Table in
     Internal SRAM. */
/* #define VECT_TAB_SRAM */
#define VECT_TAB_OFFSET  0 /*!< Vector Table base offset field.
                                   This value must be a multiple of 0x200. */

/*!
    \brief      main function
    \param[in]  none
    \param[out] none
    \retval     none
*/

int main(void)
{
    /* Vector Table Relocation in Internal FLASH */
    SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET;

    /* enable IRC40K */
    rcu_osci_on(RCU_IRC40K);
    SystemCoreClockUpdate();

    /* 设定中断优先级分组 */
    nvic_priority_group_set(NVIC_PRIGROUP_PRE4_SUB0);

    /* 开启复用时钟 */
    rcu_periph_clock_enable(RCU_AF);

    /* JTAG引脚复用成普通IO */
    gpio_pin_remap_config(GPIO_SWJ_SWDPENABLE_REMAP, ENABLE);

    cTimer5Init();
    vDelayInit();
    vCPUUsageIdleHook();

    vUart0Init();
    printf("\n\n\r");
    printf("Bootloader start.\r\n");
    printf("Bootloader software version: %s - %s\r\n", pcVersionBootloaderSoftGet(), pcVersionBootloaderDateGet());
    printf("APP software version: %s - %s\r\n", pcVersionAPPSoftGet(), pcVersionAPPDateGet());
    printf("SystemCoreClock: %d\r\n", (int32_t)SystemCoreClock);

    if(RESET != rcu_flag_get(RCU_FLAG_LPRST))
        printf("低功耗复位\r\n");
    if(RESET != rcu_flag_get(RCU_FLAG_WWDGTRST))
        printf("窗口看门狗复位\r\n");
    if(RESET != rcu_flag_get(RCU_FLAG_FWDGTRST))
        printf("独立看门狗复位\r\n");
    if(RESET != rcu_flag_get(RCU_FLAG_SWRST))
        printf("软件复位\r\n");
    if(RESET != rcu_flag_get(RCU_FLAG_PORRST))
        printf("掉电/上电复位\r\n");
    if(RESET != rcu_flag_get(RCU_FLAG_EPRST))
        printf("NRST引脚复位\r\n");
    /* 清除复位标置 */
    rcu_all_reset_flag_clear();


    /* 创建第1个用户任务 */
    xTaskCreate(vTaskSystemInit, "System Init", 1024, NULL, configMAX_PRIORITIES - 1, &g_TaskSystemInitHand);
    /* 启动 RTOS 运行 */
    vTaskStartScheduler();


    printf("\n\rFreeRTOS start error ...\r\n");

    while (1);
}

/* retarget the C library printf function to the USART */
int fputc(int ch, FILE *f)
{
   /* 等待发送寄存器发送完成，并且该串口已经在正常工作、已经使能发送功能 */
   while((RESET == usart_flag_get(USART0, USART_FLAG_TBE)) && ((USART_CTL0(USART0) & USART_CTL0_UEN) != 0) && ((USART_CTL0(USART0) & USART_TRANSMIT_ENABLE) != 0));

   /* Transmit Data */
   USART_DATA(USART0) = ch & 0xFF;

    return ch;
}

void __aeabi_assert(const char *expr, const char *file, int line)
{
    
}
